package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * 员工管理模块
 */
@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    EmployeeServiceImpl employeeService;
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest req){
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        //Employee one1 = employeeService.lambdaQuery().eq(Employee::getUsername, username).one();
        if (one==null){
            return R.error("用户名不正确");
        }
        if (!one.getPassword().equals(password))
        {
            return R.error("密码不正确");
        }
        if (one.getStatus()==0)
        {
            return R.error("员工已被禁用");
        }
        req.getSession().setAttribute("employee",one.getId());
        return R.success(one);

    }

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest req){
        req.getSession().invalidate();
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> add(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    @GetMapping("page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        Page<Employee> page1 = new Page<>(page,pageSize);
        employeeService.lambdaQuery()
                .eq(StringUtils.isNotBlank(name),Employee::getName,name)
                .orderByDesc(Employee::getUpdateTime)
                .page(page1);
        return R.success(page1);
    }
    @PutMapping
    public R<String> edit(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("编辑成功");
    }
    @GetMapping("{id}")
    public R<Employee> update(@PathVariable Long id){
        Employee employee = employeeService.lambdaQuery().eq(Employee::getId, id).one();
        return R.success(employee);
    }
}




